//
//  SuoShuQiYeViewController.m
//  EnterpriseEdition
//
//  Created by ypj on 2021/10/8.
//

#import "SuoShuQiYeViewController.h"
#import "QiYeMessageTableViewCell.h"
#import "QiYeHeaderView.h"
#import "CreateQiYeViewController.h"
#import "LoginPageViewController.h"
#import "PersonCenterViewController.h"
#import "JoinCompanyVC.h"
@interface SuoShuQiYeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)  UITableView       *  tableView;
@property(nonatomic,strong)NSMutableArray       *  dataAry;
@end

@implementation SuoShuQiYeViewController

-(void)viewWillAppear:(BOOL)animated{
     [super viewWillAppear:animated];
      
}

- (void)viewDidLoad {
   [super viewDidLoad];
    
    [self layoutCustomNav:self.view title:@"所属企业"];
    self.view.backgroundColor = White_Color;
    self.dataAry = [NSMutableArray arrayWithArray:@[@"1.开通租赁端所有权限",@"2.协助您线上管理车辆及员工",@"3.官方指定语音房间，24小时在线吸粉",@"4.车辆上架增加流量"]];
    [self createTableView];
}

-(void)createTableView{

    self.tableView = [[UITableView  alloc] initWithFrame:CGRectMake(0, LL_StatusBarAndNavigationBarHeight + 10, ScreenWidth, B_HEIGHT - LL_StatusBarAndNavigationBarHeight - LL_TabbarSafeBottomMargin - 142 - 10)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = White_Color;
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:[QiYeMessageTableViewCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[QiYeMessageTableViewCell reuseIdentifier]];
    [self.view addSubview:self.tableView];
    
    UIButton *createButton = [UIButton buttonWithType:UIButtonTypeCustom];
    createButton.frame = CGRectMake(20, ScreenHeight - 100, ScreenWidth / 2 - 20 - 8, 48);
    [createButton setTitle:@"创建企业" forState:UIControlStateNormal];
    createButton.backgroundColor = White_Color;
    [createButton setTitleColor:ColorWith3C6CFC forState:UIControlStateNormal];
    [createButton createBordersWithColor:ColorWith3C6CFC withCornerRadius:4.0 andWidth:1];
    [createButton addTarget:self action:@selector(createButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:createButton];
    
    UIButton *joinBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    joinBtn.frame = CGRectMake(ScreenWidth / 2 + 8, ScreenHeight - 100, ScreenWidth / 2 - 20 - 8, 48);
    [joinBtn setTitle:@"加入企业" forState:UIControlStateNormal];
    joinBtn.backgroundColor = ColorWith3C6CFC;
    [joinBtn createBordersWithColor:ColorWith3C6CFC withCornerRadius:4.0 andWidth:1];
    [joinBtn addTarget:self action:@selector(joinClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:joinBtn];
}

- (void)joinClick
{
    JoinCompanyVC *joinCompanyVC = [[JoinCompanyVC alloc] init];
    joinCompanyVC.isFromeLogin = self.isFromeLogin;
    [self.navigationController pushViewController:joinCompanyVC animated:YES];
}

-(void)createButtonClick{
    CreateQiYeViewController *VC =[[CreateQiYeViewController alloc] init];
    VC.isFromeLogin = self.isFromeLogin;
    [self pushToNextController:VC];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.dataAry.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
     return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QiYeMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[QiYeMessageTableViewCell reuseIdentifier]];
    if (!cell){
        cell = [[QiYeMessageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[QiYeMessageTableViewCell reuseIdentifier]];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.contentlb.text = self.dataAry[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [QiYeMessageTableViewCell cellHeight];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 100)];
    QiYeHeaderView  *View = [QiYeHeaderView shareInstance];
    View.frame = CGRectMake(0, 0, ScreenWidth, 100);
    [View.addButton whenTapped:^{
        if (View.inPutCodeTF.text.length == 0){
            showAlert(@"邀请码不能为空");
            return;
        }
    }];
    [headerView addSubview:View];
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 90;
}

-(void)backButtonClick:(UIButton *)sender{
    if (self.isFromeLogin == YES){
        if (self.isFromeTuiChu == YES) {
            //如果在该页面返回--直接退出到登录页面
            [self checkOut];
        } else {
            for (UIViewController *controller in self.navigationController.viewControllers){
                if ([controller isKindOfClass:[LoginPageViewController class]]){
                    [[SDWebImageDownloader sharedDownloader] cancelAllDownloads];
                    [[YTKNetworkAgent sharedAgent] cancelAllRequests];
                    [SSKeyChainMgr delLoginToken];
                    [YLUserMgr deleteUser];
                    LoginPageViewController *messageVC = (LoginPageViewController *)controller;
                    [self.navigationController popToViewController:messageVC animated:YES];
                }
            }
        }
    } else {
        for (UIViewController *controller in self.navigationController.viewControllers){
            if ([controller isKindOfClass:[PersonCenterViewController class]]){
                PersonCenterViewController *messageVC = (PersonCenterViewController *)controller;
                [self.navigationController popToViewController:messageVC animated:YES];
            }
        }
    }
}


-(void)checkOut{
    [[MSStorageUtils shareInstance] userDefaultSave:@"NO" withKey:USER_DEFAULT_KEY_MEMBER_IsLogin];
    [[SDWebImageDownloader sharedDownloader] cancelAllDownloads];
    [[YTKNetworkAgent sharedAgent] cancelAllRequests];
    [SSKeyChainMgr delLoginToken];
    [YLUserMgr deleteUser];
    LoginPageViewController *pageVC = [[LoginPageViewController  alloc] init];
    UINavigationController  *navigationVC = [[UINavigationController alloc] initWithRootViewController:pageVC];
    UIWindow *mainWindow = [BaseViewController getCurrentWindow];
    [mainWindow setRootViewController:navigationVC];
     showAlert(@"退出成功");
}

@end
